<?php
class Admin_TripController extends Zend_Controller_Action
{	
	protected $_session;
	public function indexAction()
	{
		
	}
	
	public function _getAdminNS()
	{
		if($this->_session == null) {
			$this->_session = new Zend_Session_Namespace('Admin');
		}	
		return $this->_session;
	}
	
	public function createMultipleAction()
	{
		$createForm = new Admin_Form_TripCreateMultiple();
		$createForm->setAction('/admin/trip/create-multiple')->setMethod('post');
		
		// add route element
		$routeModel = new Admin_Model_Route();
		$routes = $routeModel->getRouteIDRouteNameMap();
		$createForm->addRouteElement($routes);
		
		if($this->_request->isPost()) {			
			$route = $this->_request->getParam('route');
			if($createForm->isValid($_POST)) {
				$data = $createForm->getValues();
				$this->_getAdminNS()->routeID = $route;		
				$this->_getAdminNS()->tripCount = $data['tripCount'];
				$this->_getAdminNS()->dayCount = $data['dayCount'];
				$this->_getAdminNS()->startDate = $data['startDate'];
				return $this->_forward('create-multiple-cont', 'trip', 'admin');
			}
		}		
		$this->view->form = $createForm;
	}
	
	// continue to createMultiple
	public function createMultipleContAction()
	{		
		$tripTimeForm = new Admin_Form_TripCreateMultipleCont();
		$tripTimeForm->setAction('/admin/trip/create-multiple-cont')->setMethod('post');

		$tripCount = $this->_getAdminNS()->tripCount;
		
		// add trip line; trip line includes: bus, departure time, arrival time and fare
		$busModel = new Admin_Model_Bus();
		$buses = $busModel->getBusIDBusRegNumberMap();
		$tripTimeForm->addTripLines($tripCount, $this->_getAdminNS()->startDate, $buses);		
				
		$fromCreateMultiple = $this->_request->getParam('fromCreateMultiple');	
		
		if($fromCreateMultiple) {
			// emptpy
		} else {
			if($this->_request->isPost()) {						
				if($tripTimeForm->isValid($_POST)) {					
					$routeID = $this->_getAdminNS()->routeID;
					$tripCount = $this->_getAdminNS()->tripCount;
					$dayCount = $this->_getAdminNS()->dayCount;
					$startDate = $this->_getAdminNS()->startDate;
					$data = $tripTimeForm->getValues();					
					$tripLines = $data['tripLines'];							
					$tripModel = new Admin_Model_Trip();
					try {
						$r = $tripModel->createMultipleTrips($routeID, $tripCount, $dayCount, $startDate, $tripLines);
						return $this->_redirect('/admin/trip/list');
					} catch(Exception $e) {
						echo $e->getMessage(); exit();
						return TBB_Utility_Redirector::redirect('Could not create multiple trips.', 'admin', 'error', 'fatal-error');
					}
				}
			}
		}		
		$this->view->form = $tripTimeForm;
	}
	
	public function createAction()
	{
		$tripForm = new Admin_Form_Trip();
		$tripForm->setAction('/admin/trip/create')->setMethod('post');

		// add route element
		$routeModel = new Admin_Model_Route();
		$routes = $routeModel->getRouteIDRouteNameMap();
		$tripForm->addRouteElement($routes);
		
		// add bus element
		$busModel = new Admin_Model_Bus();
		$buses = $busModel->getBusIDBusRegNumberMap();
		$tripForm->addBusElement($buses);
		
		if($this->_request->isPost()) {
			// get params from $_POST
			$routeName = $this->_request->getParam('route');
			$bus = $this->_request->getParam('bus');
									
			if($tripForm->isValid($_POST))
			{
				$data = $tripForm->getValues();				
				$tripModel = new Admin_Model_Trip();
				
				// convert from Vietnamese date format to MySQL date format
				$departureTime = TBB_Utility_Date::formatDateTime($data['departureTime'], 'dd-MM-y HH:mm:ss', 'y-MM-dd HH:mm:ss');
				$arrivalTime = TBB_Utility_Date::formatDateTime($data['arrivalTime'], 'dd-MM-y HH:mm:ss', 'y-MM-dd HH:mm:ss');
								
				try {
					$tripModel->createTrip(
						$routeName,
						$bus,
						$departureTime,
						$arrivalTime,
						$data['fare']
					);
				} catch(Exception $e) {					
					return TBB_Utility_Redirector::redirect($e->getMessage(), 'admin', 'error', 'fatal-error');
				}				
				return $this->_redirect('/admin/trip/list');
			}
		}
		$this->view->form = $tripForm;
	}
	
	public function listAction()
	{
		// create search form
		$searchForm = new Admin_Form_TripSearch();
		$searchForm->setAction('/admin/trip/list')->setMethod('post');
		$this->view->searchForm = $searchForm;
		
		// get params from request
		$sorter = $this->_request->getParam('sorter', null);
		$filter = $this->_request->getParam('filter', null);
		$filterText = $this->_request->getParam('filterText', null);
		
		// populate them to form
		$searchForm->getElement('sorter')->setValue($sorter);
		$searchForm->getElement('filter')->setValue($filter);
		$searchForm->getElement('filterText')->setValue($filterText);

		// list trip
		$tripModel = new Admin_Model_Trip();		
		$tripAdapter = $tripModel->getTripPaginatorAdapter($filter, $filterText, $sorter);		
		
		if($tripAdapter) {
			$paginator = new Zend_Paginator($tripAdapter);
			$page = $this->_request->getParam('page', 1);
			$paginator->setItemCountPerPage(5);
			$paginator->setCurrentPageNumber($page);	
			$paginator->setPageRange(8);
			$this->view->paginator = $paginator;
		}
	}
	

	
	public function updateAction()
	{
		$tripForm = new Admin_Form_Trip();
		$tripForm->setAction('/admin/trip/update')->setMethod('post');
		
		// add route element
		$routeModel = new Admin_Model_Route();
		$routes = $routeModel->getRouteIDRouteNameMap();
		$tripForm->addRouteElement($routes);
		
		// add bus element
		$busModel = new Admin_Model_Bus();
		$buses = $busModel->getBusIDBusRegNumberMap();
		$tripForm->addBusElement($buses);

		if($this->_request->isPost()) {
			// get trip ID from $_POST
			$id = $this->_request->getParam('tripID');
			$routeID = $this->_request->getParam('route');
			$busID = $this->_request->getParam('bus');
					 
			if($tripForm->isValid($_POST)) {
				$data = $tripForm->getValues(); 
				$tripModel = new Admin_Model_Trip();

				// convert from Vietnamese date format to MySQL date format
				$departureTime = TBB_Utility_Date::formatDateTime($data['departureTime'], 'dd-MM-y HH:mm:ss', 'y-MM-dd HH:mm:ss');
				$arrivalTime = TBB_Utility_Date::formatDateTime($data['arrivalTime'], 'dd-MM-y HH:mm:ss', 'y-MM-dd HH:mm:ss');
				$tripModel->updateTrip(
					$data['tripID'],
					$routeID,
					$busID,
					$departureTime,
					$arrivalTime,
					$data['fare']
				);
				$this->_redirect('/admin/trip/list');
			}
		} else {
			// get trip ID from URL
			$id = $this->_request->getParam('id');
		}

		// populate form
		$tripModel = new Admin_Model_Trip();
		$tripRow = $tripModel->find($id)->current();
		if($tripRow) {					
			$tripForm->getElement('tripID')->setValue($tripRow->trip_id);
							
			// convert from MySQL date format to Vietnamese date format
			$departureTime = TBB_Utility_Date::formatDateTime($tripRow->departure_time);
			$arrivalTime = TBB_Utility_Date::formatDateTime($tripRow->arrival_time);					
			
			$tripForm->setRouteValue($tripRow->route_id);
			$tripForm->setBusValue($tripRow->bus_id);
			$tripForm->getElement('departureTime')->setValue($departureTime);
			$tripForm->getElement('arrivalTime')->setValue($arrivalTime);
			$tripForm->getElement('fare')->setValue($tripRow->fare);
		}
		
		$this->view->form = $tripForm;
	}
	
	public function deleteAction()
	{
		$id = $this->_request->getParam('id');
		$tripModel = new Admin_Model_Trip();
		try {
			$tripModel->deleteTrip($id);	
		} catch(Exception $e) {
			return TBB_Utility_Redirector::redirect($e->getMessage(), 'admin', 'error', 'fatal-error');	
		}		
		$this->_redirect('/admin/trip/list');
	}
	

}